package org.liuqm.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 通过实现 InvocationHandler接口 生成动态代理
 */
public class JDkProxy implements InvocationHandler {

    private Object target;

    public JDkProxy(Object target) {
        this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("进入jdk动态代理类");
        long startTime = System.currentTimeMillis();
        //调用被代理的方法
        Object o = method.invoke(target, args);
        long endTime = System.currentTimeMillis();
        System.out.println("调用方法" + method.getName() + "：花费了" + (endTime - startTime) + "ms");
        return o;
    }
}
